package mcommand;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import mcommand.MainPanel.MainPanelState;

/**
 * This class defines how the Game Window should look like (a board will be
 * added), how to submit the highscore and how the user can close it.
 *
 * @author Marcel, Christian, Klaus
 */
@SuppressWarnings("serial")
public class MCommand extends JFrame {

    // defines how big the game window should be
    public final int SCREEN_X = 800;
    public final int SCREEN_Y = 600;

    // if the user enters no name, then he is called in game anonym
    public String playername = "anonym";

    // initiates a temporary playername variable for comparison
    String playernametemp;

    // defines a highscore
    public Highscore highscore;
    private int highscorePlaced = 0;

    // creates a tool to read xml files
    ToolXMLReader xmlreader = new ToolXMLReader();

    /**
     * Defines how the Game Window should look like and how the user can close
     * it.
     */
    public MCommand() {
        // creates a new highscore
        highscore = new Highscore(true);

        // sets that game is not continued
        boolean gameContinued = false;

        // if a saved game was found
        if (xmlreader.checkGameStateXML()) {

            // shows options to load or not
            if ((JOptionPane.showConfirmDialog(this,
                    "Es wurde ein gespeichertes Spiel gefunden."
                    + "Dieses fortsetzen?")) == JOptionPane.OK_OPTION) {

                // sets that this is a continued game
                gameContinued = true;
            } else {

                // if not, a emty game state is written into xml file
                ToolXMLWriter.writeEmptyGameStateIntoXML();
            }
        }

        // if the game is continued, it loads the playername from xml file
        if (gameContinued) {
            playername = xmlreader.readPlayernameFromXML();
        } else {
            // gets the playernametemp from the player via a input dialog
            playernametemp = JOptionPane.showInputDialog("Spielername");

            // compares if the user has entered a playername,
            // if he entered nothing the standard name will be taken
            if (playernametemp != null) {
                playername = playernametemp;
            }
        }

        // adds to the window a new board object
        Board board = new Board(this, gameContinued);

        // sets playername
        board.setPlayername(playername);

        // adds the board to the window
        this.add(board);

        // sets the window size
        this.setSize(SCREEN_X, SCREEN_Y);

        // sets the title for the window
        this.setTitle("MCommand");

        // places the window in the center of the users screen
        this.setLocationRelativeTo(null);

        // the user can now not change the window size - better for always the
        // same layout quality
        this.setResizable(false);

        // add Windows Listener to react on close
        addWindowListener(new OnCloseListener());

        // now shows the window for the user
        this.setVisible(true);
    }

    /**
     * Submits the highscore.
     * 
     * @param playername playername
     * @param points points
     */
    public final void submitHighscore(final String playername,
            final int points) {
        // calls the addHighscore method of highscore
        highscorePlaced = highscore.addHighscore(new HighscoreUnit(playername,
                points));

        // initiates a empty string
        String message = "";

        // if the score is in the top 10
        if (highscorePlaced != 0) {
            message = "Sie haben Platz " + highscorePlaced + " erreicht.";

        // if the score is not under the top ten
        } else {
            message = "Sie haben es leider nicht in die Highscore geschafft.";
        }

        // sets that there is no savegame if a game is ended
        ToolXMLWriter.writeEmptyGameStateIntoXML();

        // shows the message
        JOptionPane.showMessageDialog(this, message);
    }

    /**
     * If the window closes, a new startMenu will be created, instantly jumps to
     * the highscore Screen.
     * 
     */
    private class OnCloseListener extends WindowAdapter {
        /**
         * If the window closes, a new statistic is written
         * into the xml file for statistic and a new
         * start menu with the highscore is shown.
         * 
         * @param e e
         */
        public void windowClosing(final WindowEvent e) {
            Statistics.writeStatisticsIntoXML();
            new StartMenu(MainPanelState.HIGHSCORE);
        }
    }
}
